/**
 * 
 */
package systole.view.crud.pathology;

import java.awt.Frame;
import java.util.Calendar;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Pathology;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;
import systole.view.messages.EntityDescriptions;

/**
 * @author jmj
 *
 */
public class ControllerPathologyEdition extends ControllerEntityEdition {

    /**
     * 
     */
    public ControllerPathologyEdition() {
        super();
        this.entityName = EntityDescriptions.PATHOLOGY;
        this.newDescription = "Nueva";
    }


    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        Pathology pathologyToLoad = (Pathology) this.curretnEntity;
        JDialogPathology formToLoad = (JDialogPathology) this.editForm;

        pathologyToLoad.setName(formToLoad.getjEdtName().getText());
        pathologyToLoad.setInitials(formToLoad.getjEdtInitials().getText().isEmpty() ? null : formToLoad.getjEdtInitials().getText());
        pathologyToLoad.setDescription(formToLoad.getjTxtDescription().getText());
        pathologyToLoad.setLastUpdate(Calendar.getInstance());

    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogPathology formToLoad = (JDialogPathology) this.editForm;
        Pathology currentPathology = (Pathology) this.curretnEntity;
        formToLoad.getjEdtName().setText(currentPathology.getName());
        formToLoad.getjEdtInitials().setText(currentPathology.getInitials());
        formToLoad.getjTxtDescription().setText(currentPathology.getDescription());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogPathology formToValid = (JDialogPathology) this.editForm;
        Pathology pathology = (Pathology) this.curretnEntity;
        if ((formToValid.getjEdtName().getText() == null) || (formToValid.getjEdtName().getText().isEmpty())) {
            return "Debe Ingresar el nombre de la patología";
        }

        try {

            if (this.facadeDB.getPathologyBroker().existPathologyByName(formToValid.getjEdtName().getText(), pathology.getId())) {
                return "Ya existe una patología con el nombre ingresado";
            }

            if ((formToValid.getjEdtInitials().getText() != null) && (!formToValid.getjEdtInitials().getText().isEmpty())) {
                if (this.facadeDB.getPathologyBroker().existPathologyByInitials(formToValid.getjEdtInitials().getText(), pathology.getId())) {
                    return "Ya existe una patología con la sigla ingresada";
                }
            }

        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
        if (this.isEditing()) {
            this.facadeDB.getPathologyBroker().update((Pathology) this.curretnEntity);
        } else {
            this.facadeDB.getPathologyBroker().insert((Pathology) this.curretnEntity);
        }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/medic/pathology16.png"));
    }

    @Override
    protected Object createEntity() {
        return new Pathology();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogPathology(parent, this);
    }
}
